package com.group2.bankonline.controller;

import com.group2.bankonline.pojo.Account;
import com.group2.bankonline.pojo.Transrecord;
import com.group2.bankonline.service.IAccountService;
import com.group2.bankonline.service.ITransrecordService;
import com.group2.bankonline.utils.*;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  交易记录前端控制器
 * </p>
 *
 * @author syf
 * @since 2024-07-11
 */
@RestController
@RequestMapping("/mobilebank/transrecord")
public class TransrecordController {
    @Autowired
    private ITransrecordService iTransrecordService;

    @Autowired
    private IAccountService iAccountService;

    /*
    * 查询账户所有交易记录
    * */
    @RequestMapping("/queryAll")
    public ResponseJson getByAidOrOid(@RequestBody Account account){
        String name = account.getAccountname();
        int id = iAccountService.getByAccountName(name);
        List<Transrecord> transrecords=iTransrecordService.getByAidOrOid(id);
        if(transrecords==null){
            return new ResponseJson("400","您暂未有交易记录。");
        }else{
            return new ResponseJson("200","查询成功，记录如下",transrecords);
        }
    }
    /*根据交易类型查询交易记录*/
    @RequestMapping("/type")
    public ResponseJson getByType(@RequestBody RequestType requestType){
        String name = requestType.getAccount().getAccountname();
        int id = iAccountService.getByAccountName(name);
        int transtypeId = requestType.getTranstypeId();
        List<Transrecord> transrecords = iTransrecordService.getByType(id,transtypeId);
        if(transrecords==null){
            return new ResponseJson("400","您暂未有交易记录。");
        }else{
            return new ResponseJson("200","查询成功，记录如下",transrecords);
        }
    }

/*
* 根据交易金额范围查询
* */
    @RequestMapping("/money")
    public ResponseJson getByMoney(@RequestBody RequestMoney requestMoney){
        String name = requestMoney.getAccount().getAccountname();
        int id = iAccountService.getByAccountName(name);
        int money1 = requestMoney.getMoney1();
        int money2 = requestMoney.getMoney2();
        List<Transrecord> transrecords = iTransrecordService.getByMoney(id,money1,money2);
        if(transrecords==null){
            return new ResponseJson("400","您暂未有交易记录。");
        }else{
            return new ResponseJson("200","查询成功，记录如下",transrecords);

        }
    }
/*
* 以交易日期查询交易记录
* */
    @RequestMapping("/date")
    public ResponseJson getByDate(@RequestBody RequestDate requestDate){
        String name = requestDate.getAccount().getAccountname();
        int id = iAccountService.getByAccountName(name);
        String date = requestDate.getTransDate();
        List<Transrecord> transrecords = iTransrecordService.getByDate(id,date);
        if(transrecords==null){
            return new ResponseJson("400","您暂未有交易记录。");
        }else{
            return new ResponseJson("200","查询成功，记录如下",transrecords);
        }
    }

/*
* 以收款人查询交易记录
* */
    @RequestMapping("/oid")
    public ResponseJson getByOid(@RequestBody RequestAccount requestAccount){
        String aname = requestAccount.getAccountname();
        int aid = iAccountService.getByAccountName(aname);
        int oid = iAccountService.getByAccountName(requestAccount.getAccountname());
        List<Transrecord> transrecords = iTransrecordService.getByAidAndOid(aid,oid);
        if(transrecords==null){
            return new ResponseJson("400","您暂未有交易记录。");
        }else{
            return new ResponseJson("200","查询成功，记录如下",transrecords);
        }
    }

}
